/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.backbean;

import com.bice.controller.entity.BiceUser;
import com.bice.controller.entity.Post;
import com.bice.controller.service.BiceUserService;
import com.bice.util.annotation.SessionResource;
import com.bice.util.annotation.SpringServiceResource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Sirius
 */
@ManagedBean
@ViewScoped
public class RegisterBean extends BackBean {
    
    private BiceUser user;
    private String confirmPassword;
    private boolean errorRegister;
    
    @SpringServiceResource
    private transient BiceUserService biceUserService;
    @SessionResource
    private transient SessionBean session;

    /**
     * Creates a new instance of RegisterBean
     */
    public RegisterBean() {
        user = new BiceUser();
    }
    
    public BiceUser getUser() {
        return user;
    }

    public boolean isErrorRegister() {
        return errorRegister;
    }
    
    public void register() {
        errorRegister = false;
        if(confirmPassword != null && confirmPassword.equals(user.getPassword())) {
            biceUserService.save(user.getNickname(), user.getNickname(), encrypt(user.getPassword()));
            session.setUser(user);
        } else {
            errorRegister = true;
        }
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }
    
}